﻿var g_vWeightStrs=[];
var g_vWeightXmlDoc;
function OnWeightFlashReady(id)
{
    //setTimeout("loadWeightFlashData('" + id + "')", 1000);
 
   loadWeightFlashData(id);
}
function getWeightFlash()
{
    if (typeof(g_vIndicatorFlash)=="undefined") 
    {
        g_vWeightFlash="weightchart";
    }
    return g_vWeightFlash;
}
function printData(iPageNo, iPageNum, iTotal)
{
    var oParent = _$("weight_list");	
    if (oParent==null) return ;
    var xResult=g_vWeightXmlDoc.getElementsByTagName("result")[0];
    var xHeight = xResult.getElementsByTagName("height")[0];
    var iHeight = xHeight.getAttribute("value");
    if(iHeight==0) 
    {
        ShowRangeDisplay(["ID_HEIGHT","ID_MODITY_HEIGHT","ID_CANCEL_HEIGHT"],false);
        ShowRangeDisplay(["ID_INPUT_HEIGHT","ID_ADD_HEIGHT"],true);
    }
    else
    {
        _$("ID_HEIGHT").innerHTML = iHeight ;
    }
    if( xResult.getElementsByTagName("history").length == 0  ) 
    {
        ShowRangeDisplay(["weight_list", "ID_page"], false)
    }
    else
    {
        var sHtml = document.body.innerHTML.between("<!--TP.LIST","TP.LIST-->");
        var mapName=[
				['id','<%id%>'],
				['value','<%value%>'],
				['date','<%date%>',function(v){return parseDate(v).toCommonString()}]
				];
		var iStart = (iPageNo-1)*iPageNum ;
		var iEnd   = iStart + iPageNum ;
		iEnd = iEnd > iTotal ? iTotal : iEnd;
		
        showXmlNode(xResult, oParent, "history",sHtml, mapName, iStart, iEnd);
    }
    iTotalPageNum = Math.ceil(iTotal/iPageNum);      
    showPage(iPageNo,iTotalPageNum,function(n){return "javascript:printData("+n+","+iPageNum+","+iTotal+");";},document.getElementById("ID_page"),0);
}
function formatWeightFlashData(startDate,endDate, bForce)
{
    g_vWeightStrs=[];
    var xResult = g_vWeightXmlDoc.getElementsByTagName("result")[0];
    var iHeight = xResult.getElementsByTagName("height")[0].value ;
    var xHistories = xResult.getElementsByTagName("histories")[0];
    var xHistory=xResult.getElementsByTagName("history");
    var bgcolor='"bg_colour": "#FFFFFF"';
    var now = parseDate(xResult.getAttribute("now"));
    printData(1,10, xHistory.length);
    
     if (typeof(endDate)=="undefined") endDate = now ;
    if(typeof(startDate)=="undefined") {startDate = parseDate(xResult.getAttribute("diseasestart"));bForce=false;}
    var startValue = 0 ;
    //format 
    var sTitle='"title":{"text":"体重记录","style": "{font-size: 20px;color:#0000ff;font-family:Verdana;text-align:center}"}';
    //var sYLegend='"y_legend":{"text":"重量(kg)","style": "{color:#0000ff;font-size:15px}"}';
    var sXAxis='';
    var sElement='';
   
    var YAxis =[0, 40, 80, 120, 160, 200] ;
    var sYAxis ='"y_axis":{"max":5,"step":1,"labels":["'+YAxis.join('","')+'"] }';
    var curDate = now ;
    if (xHistory!=null && xHistory.length > 0) 
    {
        curDate = parseDate(xHistory[0].getAttribute("date")) ;
        if (typeof(bForce)=="undefined" || !bForce) 
        {
            if ( curDate<startDate ) startDate = curDate ;
        }
    }
    var xLabels=[];
    iCur = 4 ;
    var vXDistance = Math.ceil(distanceDate(startDate,endDate)/(iCur-1.0));//alert("distance:"+distanceDate(startDate,endDate)+"unit:"+vXDistance);
  
    curDate = endDate;
    do 
    {
        xLabels[iCur] = curDate.toCommonString();
        startDate = curDate ;	
        curDate = getNextDay(curDate, -vXDistance);
    } while(--iCur >=0 );
    iCur=0;
    var xUnit = (distanceDate(startDate, endDate)+0.0)/(xLabels.length-1) ;
    var sXAxis ='"x_axis":{ "max":'+(xLabels.length-0.8)+',"labels":{"rotate":0,"labels":["'+xLabels.join('","')+'"]}}';
    var Elements = [] ;
    for(var i=0; i<xHistory.length; ++i)
    {
        var date = parseDate(xHistory[i].getAttribute("date"));
        if (date < startDate) continue;
        if (date > endDate ) break;
        curDistance = distanceDate(startDate, date);
        var yValue = getValueByArray(YAxis, (parseFloat(xHistory[i].getAttribute("value"))));
        var xValue = curDistance/xUnit;
        var sElement ='{"value":'+yValue+',"colour":"#736AFF","tip":"'+xHistory[i].getAttribute("value")+"kg<br>"+date.toCommonString()+'","x":'+xValue+'}';
        Elements.push(sElement);
    }
    sElements = '"elements":[{"type":"line_dot","text":"体重(单位 kg)","font-size":12,"width":2,"dot-size": 4, "halo-size":1,"values":['+Elements.join(',')+']}]';
    //document.write('{'+sTitle+','+sYLegend+','+sXAxis+','+sYAxis+','+sElements+'}');
    g_vWeightStrs.push('{'+bgcolor+','+sXAxis+','+sYAxis+','+sElements+'}'); 
}
function showWeightFlash(id)
{
    if (g_vWeightStrs.length<id) 
        return ;
    if (0==id) 
    {
        for(var i=1;i<=g_vWeightStrs.length;++i)
        {
            var tmp = _$(getWeightFlash()+i);
            tmp.load(g_vWeightStrs[i-1]);
        }
    }
    else
    {
        var tmp = _$(getWeightFlash()+id);
        try{tmp.load(g_vWeightStrs[id-1]);}
        catch(e){}
    }
   
}
function loadWeightFlashData(id)
{	
	function OnSucc(xmlDoc)
    {  
        g_vWeightXmlDoc = xmlDoc ;
        formatWeightFlashData();
        showWeightFlash(id);
    }
    function OnFail(xmlDoc)
    {
        alert("获取数据失败");
    }
    if (typeof(g_sWeightUrl)=="undefined") g_sWeightUrl="/Pages/Alpha/WeightHistory/WeightHistory.aspx";
    getResultXml(g_sWeightUrl,OnSucc,OnFail,OnFail);
}

function showWeightFlashMode(i,dStart,dEnd)
{
    var enddate = g_vNow;
    var startdate;
    switch(i)
    {
        case 1:startdate = getPreBeginMonthDate(enddate,3);break;
        case 2:startdate = getPreBeginMonthDate(enddate,6);break;
        case 3:startdate = getPreBeginMonthDate(enddate,12);break;
        case 4:startdate = getPreBeginMonthDate(enddate,36);break;
        case 5:
            startdate = dStart < dEnd ?　dStart : dEnd ;
            enddate = dStart  > dEnd ?　dStart : dEnd ;
            if (startdate==enddate) 
            {
                alert("起始日期和终止日期相同");
                return ;
            }
        default:break;
    } 
    formatWeightFlashData(startdate,enddate, true);
    showWeightFlash(1);
}
